//
//  HSFeedbackViewController.swift
//  HomeSchoolDemo
//
//  Created by rimi on 16/10/8.
//  Copyright © 2016年 黄华强. All rights reserved.
//

import UIKit
import SVProgressHUD

class HSFeedbackViewController: UIViewController,UITextViewDelegate {
    
    
    @IBOutlet weak var titleTF: UITextField!
    @IBOutlet weak var placeholderLabel: UILabel!
    @IBOutlet weak var contentTextView: UITextView!
    var cursorPosition:CGPoint?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        view.addSubview(myCancelButton)
        contentTextView.delegate = self
        // Do any additional setup after loading the view.
    }
    
    func textViewDidBeginEditing(_ textView: UITextView) {
        placeholderLabel.isHidden = true
    }
    
    func textViewDidEndEditing(_ textView: UITextView) {
        
        if contentTextView.text == "" {
            placeholderLabel.isHidden = false
        }
    }
    
    func textViewDidChange(_ textView: UITextView) {
        cursorPosition = textView.caretRect(for: (textView.selectedTextRange?.start)!).origin
        myCancelButton.frame = CGRect(x: UIScreen.main.bounds.width - 16, y: (cursorPosition?.y)! + contentTextView.frame.minY, width: 16, height: 16)
        if contentTextView.text == "" {
            myCancelButton.isHidden = true
        } else {
            myCancelButton.isHidden = false
        }
        
        
    }
    
    lazy var myCancelButton: UIButton = {
        let btn = UIButton(frame: CGRect(x: 0, y: 0, width: 16, height: 16))
        //        btn.setBackgroundImage(UIImage(named: "feedbackBtn_h"), for: .highlighted)
        //        btn.setBackgroundImage(UIImage(named: "feedbackBtn_n"), for: .normal)
        btn.setImage(UIImage(named: "清除"), for: .normal)
        btn.addTarget(self, action: #selector(cancelBtnClicked), for: .touchUpInside)
        btn.isHidden = true
        return btn
    }()
    
    func cancelBtnClicked() {
        contentTextView.text = ""
        myCancelButton.isHidden = true
    }
    
    @IBAction func backButtonClicked(_ sender: UIButton) {
        dismiss(animated: true, completion: nil)
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    @IBAction func submitButtonClicked(_ sender: UIButton) {
        guard let tltle = titleTF.text, tltle != "" else {
            SVProgressHUD.showInfo(withStatus: "请输入标题")
            return
        }
        
        guard let content = contentTextView.text, content != "" else {
            SVProgressHUD.showInfo(withStatus: "请输入内容")
            return
        }
        submitData()
        
    }
    
    func submitData() {
        let url = "ZH-schoolname-S-submitJXOpinion"
        let key = userInfo["key"] as! String
        let myTltle = titleTF.text!
        
        let content = contentTextView.text!
        
        let param = ["key":key,"title":myTltle,"content":content] as [String : Any]
   
        HSNetWorkTools.shared.downloadData(url: url, parameters: param) { (data, error) in
            guard let data = data as? NSDictionary else {
                SVProgressHUD.showInfo(withStatus: "网络请求错误，提交失败!")
                return
            }
            
            if data["result"]! as! Int == 1 {
                SVProgressHUD.showInfo(withStatus: "反馈提交失败")
            } else {
                SVProgressHUD.showInfo(withStatus: "意见反馈提交成功")
            }
            
        }
        
    }
    
}
